﻿using BYSJ.BLL;
using BYSJ.IBLL;
using BYSJ.Model;
using BYSJ.Web.Commmend;
using BYSJ.Web.Filters;
using BYSJ.Web.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BYSJ.Web.Controllers
{
    [AdminFilter]
    [ShenFen]
    public class oneselfController : Controller
    {
        
        /// <summary>
        /// 个人中心控制器
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public ActionResult Index(string Name)
        {
            string name = Name;
            IUserSetService us = new UserSetService();
            var user = us.LoadEntities(u => u.Name == Name).ToList().First();
            oneSelf oneself = new ConvertmModel().aboutMe(user);
            return View(oneself);
        }
        public string xiugaiAccount(string password)
        {
            if (password.Length >= 6)
            {
                IUserSetService userServce = new UserSetService();
                UserSet us = new Model.UserSet();
                int Id = Convert.ToInt32(Request.Cookies["cookiename"].Value);
                //查出实体
                us = userServce.LoadEntities(u => u.id == Id).ToList().First();
                //更改对象
                us.PassWord = password;
                userServce.Update(us);
                return "ok";
            }
            else
            {
                return "修改失败";
            }
        }
        public string touXiang(HttpPostedFileBase croppedImage, string demo)
          {
              FileDele file = new FileDele();
              IUserSetService userServce = new UserSetService();
              UpYun upyun = new UpYun("cnkifile", "senhong", "sen123456");
              string filePath = null;
              try
              {
                bool b = false;
                  if (croppedImage != null && croppedImage.ContentLength > 0)
                  {
                      //拼接出来文件名字
                      string fileName = DateTime.Now.ToString("yyyyMMddmmss") + "-" + Path.GetFileName(croppedImage.FileName +".png");
                      //文件完整路径
                       filePath = Path.Combine(Server.MapPath("~/demo"), fileName);
                      //保存至磁盘
                      croppedImage.SaveAs(filePath);
                      FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                      BinaryReader r = new BinaryReader(fs);
                      byte[] postArray = r.ReadBytes((int)fs.Length);
                      b = upyun.writeFile("/bysjtx/" + fileName, postArray, true);
                      //完整URL 要插入数据库
                      string fileurl = "http://cnkifile.b0.upaiyun.com/bysjtx/" + fileName;
                      UserSet us = new Model.UserSet();
                      int Id =Convert.ToInt32(Request.Cookies["cookiename"].Value);
                      //查出实体
                      us= userServce.LoadEntities(u => u.id == Id).ToList().First();
                      //更改对象
                      us.ImagePsth = fileurl;
                      userServce.Update(us);
                     
                 }
                if(b==true)
                {
                    return "成功了！！";
                }
               else
                {
                    return "修改失败，请刷新重试！";
                }
              }
              catch(Exception e)
              {
                  return "修改失败，请刷新重试！！"+e.ToString();
              }
       }


       

    }
}
